Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of bare Async and Sync blocks when a scheduler is defined. #340

Merged
merged 5 commits into from
Aug 23, 2024

Conversation

ioquatix
Copy link
Member

@ioquatix ioquatix commented Aug 18, 2024

Nested fibers can cause issues with Async, when spawning child tasks within them. I was affected by this issue when trying to create a streaming response from a Rails controller, using the proc do |stream| ... Async{....} end style response body. However, protocol-rack uses a normal fiber to wrap streaming responses and the child Async task has no parent, causing it to try and create a new event loop... meltdown ensues.

Ideally, Async and Sync helpers should work when any Fiber.scheduler is defined. Right now, it's unrealistic to expect Async::Task to work in any scheduler, but at the very least, the following should work:

reactor = Async::Reactor.new # internally calls Fiber.set_scheduler

# This should run in the above reactor:
Async do
  puts "Hello World"
end

In order to do this, bare Async and Sync blocks should use Fiber.scheduler as a parent if possible.

Types of Changes

  • Bug fix.

Contribution

@ioquatix ioquatix changed the title Use fiber storage for nested fibers. Better handling of bare Async and Sync blocks. Aug 19, 2024
@ioquatix ioquatix changed the title Better handling of bare Async and Sync blocks. Better handling of bare Async and Sync blocks when a scheduler is defined. Aug 19, 2024
@ioquatix ioquatix merged commit b5494cc into main Aug 23, 2024
52 of 57 checks passed
@ioquatix ioquatix deleted the nested-fiber branch August 23, 2024 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant